package edu.winona.csclub.testing.gui;

import java.util.HashMap;

import javax.swing.table.AbstractTableModel;

import edu.winona.csclub.testing.core.Test;
import edu.winona.csclub.testing.core.Value;


public class TestsTableModel extends AbstractTableModel {

	private static final String[] columnNames = {
		"Test #",
        "Command",
        "Result",
        "Correct"
        };
	
	TestsView tests;
	public static HashMap<Test,Boolean> map = new HashMap<Test,Boolean>();

	public String getColumnName(int column){
		return columnNames[column];
	}
	
	public boolean isCellEditable(int rowIndex, int columnIndex){
		return columnIndex==3;
	}
	
	public TestsTableModel(TestsView t){
		tests=t;
	}
	
	public int getColumnCount() {
		return 4;
	}

	public int getRowCount() {
		if(tests==null)return 0;
		if(tests.getTests()==null)return 0;
		if(tests.getTests().size()==0) return 0;
		return tests.getTests().size();
	}

	public Object getValueAt(int rowIndex, int columnIndex) {
		switch( columnIndex ){
		case 0:
			return ""+rowIndex;
		case 1:
			String ret = tests.getTests().get(rowIndex).getCommand().getValue();
			for( Value v : tests.getTests().get(rowIndex).getVariables() ){
				String id = v.getVariable().getId();
				int ind = ret.indexOf(id);
				String pref = ret.substring(0,ind);
				String post = ret.substring(ind+id.length());
				ret=pref+v.getValue()+post;
			}
			ret=ret.substring(ret.indexOf(" ")+1);
			ret=ret.substring(ret.indexOf(" ")+1);
			return ret;
		case 2:
			return tests.result.get(tests.getTests().get(rowIndex))==null?"":tests.result.get(tests.getTests().get(rowIndex)).getResult();
		case 3:
			return map.get(tests.getTests().get(rowIndex))==null?new Boolean(false): map.get(tests.getTests().get(rowIndex));
		}
		return "";
	}
	
    public Class getColumnClass(int c) {
        return getValueAt(0, c).getClass();
    }
	
    public void setValueAt(Object value, int row, int col) {
    	if( col == 3);
    	map.put(tests.getTests().get(row), (Boolean) value );
  //      fireTableCellUpdated(row, col);
    }

}
